Below are a set of code examples demonstrating the use of the basic patterns discussed in Chapter 3 of "The Story of C++". The patterns are also summarized in in their own document.
The first program demonstates the basic use of the "while with a priming read", 'sum', and 'counter' patterns. Make sure you understand what value(s) act as the sentinel value(s).
// A program to add up and count expenses
// A demo of the use of a while loop with a priming read
// and of the sum pattern
// file: ch3xmpl1.cpp
#include <iostream.h>>
void main()
{
// Purpose: A program to add up and count expenses
// Receives: NONE
// Returns: NONE
double expense;
double totalExpenses = 0;
int numExpenses = 0;
cout << "Please enter an expense\n";
cout << "Enter a negative number when finished\n";
cin >> expense;
while (expense >= 0)
{ totalExpenses += expense;
numExpenses++;
cout << "Please enter an expense\n";
cout << "Enter a negative number when finished\n";
cin >> expense;
}
cout << "There were " << numExpenses << " expenses totaling $"
<< totalExpenses << endl;
}
The second example involves the same problem as the first but this time there are two inputs -
expense codes as well as expenses. Note that only the expense code has a sentinel value and therefore only it
is input before the while test. For more discussion of this see Section VIII in "The Story of C++".
// A program to add up and count expenses using two inputs
// - a one character expense code followed by the amount of the expense
// A demo of the use of a while loop with a priming read
// and of the sum pattern
// file: ch3xmpl2.cpp
#include <iostream.h>
void main()
{
// Purpose: A program to add up and count expenses using two inputs
// Receives: NONE
// Returns: NONE
char expenseCode;
double expense;
double totalExpenses = 0;
int numExpenses = 0;
cout << "Please enter an expense code\n";
cout << "Enter an '*' when finished\n";
cin >> expenseCode;
while (expenseCode != '*')
{ cout << "Please enter an expense\n";
cin >> expense;
totalExpenses += expense;
numExpenses++;
cout << "Please enter an expense code\n";
cout << "Enter an '*' when finished\n";
cin >> expenseCode;
}
cout << "There were " << numExpenses << " expenses totaling $"
<< totalExpenses << endl;
}
The third example uses the same code only adds in the calculation of the average expense. The one new part to this program is the
'if' statement that makes sure there is no division by zero.
// A program to calculate the average of some expenses using two inputs
// - a one character expense code followed by the amount of the expense
// file: ch3xmpl3.cpp
#include <iostream.h>
void main()
{
// Purpose: A program to calculate the average of some expenses using two inputs
// Receives: NONE
// Returns: NONE
char expenseCode;
double expense;
double totalExpenses = 0;
int numExpenses = 0;
double avgExpense;
cout << "Please enter an expense code\n";
cout << "Enter an '*' when finished\n";
cin >> expenseCode;
while (expenseCode != '*')
{ cout << "Please enter an expense\n";
cin >> expense;
totalExpenses += expense;
numExpenses++;
cout << "Please enter an expense code\n";
cout << "Enter an '*' when finished\n";
cin >> expenseCode;
}
if (numExpenses > 0)
{ avgExpense = totalExpenses / numExpenses;
}
else
{ avgExpense = 0;
}
cout << "The average Expense was $" << avgExpense;
}